//
//  SettingController.m
//  DYLYJ
//
//  Created by JM on 15/11/13.
//  Copyright © 2015年 JM. All rights reserved.
//

#import "SettingController.h"
#import "DYUIKit.h"
#import "DYSettingKeys.h"
#import "DYRequestAll.h"
#import "AppDelegate.h"
@interface SettingController ()
@property(nonatomic,strong)IBOutlet UISwitch *soundSwitch;
@property(nonatomic,strong)IBOutlet UISwitch *shakeSwitch;
@property(nonatomic,strong)IBOutlet UITextField *phoneTextfield;
@property(nonatomic,strong)IBOutlet UITextField *emailTextfield;
@end

@implementation SettingController

- (void)viewDidLoad {
    [super viewDidLoad];
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem backBarItemWithTarget:self action:@selector(clickBack:)];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.soundSwitch.on = [DYSettingTool boolForKey:DYSettingSoundSwitch];
    self.shakeSwitch.on = [DYSettingTool boolForKey:DYSettingShockSwitch];
    
    self.soundSwitch.onTintColor = UIColorFromHexStr(@"#ffb100");
    self.shakeSwitch.onTintColor = UIColorFromHexStr(@"#ffb100");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    UserDetailRequest * request = [UserDetailRequest request];
    request.DY_token = myAppDelegate.token;
    [request requstWithCompletionHandler:^(DYResponse *rs, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (error){
            [MJAlertView showMessage:error.localizedDescription];
            return ;
        }
        UserDetailResponse *response = (UserDetailResponse *)rs;
        NSLog(@"%@",response.DY_userInfo);
        self.emailTextfield.text = response.DY_userInfo.email;
        self.phoneTextfield.text = response.DY_userInfo.phone;
        
    }];
}

- (void)clickBack:(id) sender {
    
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 2){
//        BlindEmailRequest *request = [BlindEmailRequest request];
//        request.DY_token = myAppDelegate.token;
//        request.DY_email = @"";
//    }
//}

- (IBAction)swichValueChange:(UISwitch*)sender {
    [DYSettingTool setBool:sender.on forKey:DYSettingSoundSwitch];
}

//震动开关
- (IBAction)zhenDongSwichValueChange:(UISwitch*)sender {
    [DYSettingTool setBool:sender.on forKey:DYSettingShockSwitch];
}

- (IBAction)clickLogOut:(id)sender {
    LogoutRequest *request = [LogoutRequest request];
    request.DY_uid = myAppDelegate.uid;
    [request requstWithCompletionHandler:^(DYResponse *rs, NSError *error) {
        if (error) {
            [MJAlertView showMessage:error.localizedDescription];
            return ;
        }
        myAppDelegate.uid = nil;
        myAppDelegate.token = nil;
        myAppDelegate.islogin = NO;
        [DYSettingTool setObject:@"" forKey:DYLoginPwd];
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }];
}


@end
